#!/usr/bin/python3
import sys
import requests
import os


endport = "#{uri}"
# HTTP endpoint for authentication
AUTH_ENDPOINT = endport+"/client/disconnect"

print(AUTH_ENDPOINT)


def vpnDisconnect(username, receiveByte,sendByte):
    try:
        # Make an HTTP request to the authentication endpoint
        response = requests.get(AUTH_ENDPOINT, params={'username':username,'receiveByte':receiveByte,'sendByte':sendByte})

        # Check if the response status code is 200
        if dict(response.json()).get("code") == 200:
            print("OK")
            exit(0)  # Authentication succeeded
        else:
            print(dict(response.json()).get("code"))
            exit(1)  # Authentication failed
    except Exception as e:
        print("Error during authentication: {}".format(str(e)))
        exit(1)

if __name__ == "__main__":
    # Read the username and password from OpenVPN
    username = os.environ.get('username')
    receiveByte = os.environ.get('bytes_received')
    sendByte = os.environ.get('bytes_sent')
    # Perform custom authentication logic
    vpnDisconnect(username,receiveByte,sendByte)
